#include<bits/stdc++.h>
using namespace std;
const int N=10005;
int n,a[N],ans,b[N],fa[N],vis[N];
void check(){
	int sum=0;
	for(int i=1;i<=n;i++){
		sum+=a[b[i]];
		fa[a[b[i]]]=fa[a[b[i]]]+a[b[i]];
	}
	ans=max(ans,sum);
}
void dfs(int x){
	if(x>n){
		int c[N];memset(c,0,sizeof c);
		for(int i=1;i<=n;i++)
			c[i]=fa[i];
		check();
		for(int i=1;i<=n;i++)
			fa[i]=c[i];
		return ;
	}
	for(int i=1;i<=n;i++){
		if(vis[i]==1) continue ;
		vis[i]=1;
		b[x]=i;
		dfs(x+1);
		b[x]=0;
		vis[i]=0;
	}
	return ;
}
signed main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	freopen("light.in","r",stdin);
	freopen("light.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<n;i++){
		int u,v;
		cin>>u>>v;
		fa[u]=v;
		fa[v]=u;
	}
	dfs(1);
	cout<<ans<<endl;
	return 0;
}

